const express = require('express')


var num = 0


var _app = express()
    // 设置跨域允许所有都能访问
    _app.use((req, res, next) => {
      res.header('Access-Control-Allow-Origin', '*')
      res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
      res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
      res.setHeader("Access-Control-Max-Age", "3600"); // 1小时内无需再次预检
      if (req.method === 'OPTIONS') {
        res.sendStatus(204);
        return;
      }
      next()
    })
    // JSON解析
    _app.use(express.json())
    // 添加请求日志中间件
    _app.use((req, res, next) => {
      console.log(`${new Date().toISOString()} ${req.method} ${req.url}`)
      next()
    })
  // 根路由 - 返回 index.html
_app.get('/add', (req, res) => {
        num++
        res.send('ok'+ num)
    });

    var _server = _app.listen(7000, () => {
      console.log(`HTTP server is running on port 7000`)
    })